import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';

import { ModalController, NavParams, IonSlides, Platform } from '@ionic/angular';
import { AlertController } from '@ionic/angular';

import { File } from '@ionic-native/file/ngx';
@Component({
    templateUrl: 'img.page.html',
    styleUrls: ['upload-image.component.scss']
})
export class ImgPage implements OnInit, AfterViewInit {
    data: any;
    pdfData: any;
    activeIndex: number;
    currentImgIndx: number;
    totalImgIndx: number;
    forPdf = false;
    slides: any[];
    isNative: boolean;
    sliderHome: any;
    slideOptsHome = {
        autoplay: false,
        effect: 'flip'
    };
    isShowDel = true;
    @ViewChild('loopSlider') loopSlider: IonSlides;

    constructor(
        private navParams: NavParams,
        private modalCtrl: ModalController,
        public router: Router,
        private alert: AlertController,
        private file: File,
        private plt: Platform
    ) {
    }

    ngOnInit() {
        this.forPdf = this.navParams.data.forPdf;
        if (this.forPdf) {
            this.data = JSON.parse(this.navParams.data.value).valueLongArray;
            this.pdfData = JSON.parse(this.navParams.data.value);
        } else {
            this.data = JSON.parse(this.navParams.data.value);
        }
        this.activeIndex = this.navParams.data.index;
        if (this.navParams.data.isShowDel !== undefined) {
            this.isShowDel = this.navParams.data.isShowDel;
        }
        console.log(' message-----> 37 ImgPage:data= ' + JSON.stringify(this.data) + '  activeIndex =' + this.activeIndex);
        this.sliderHome = {
            slidesItems: this.data

        };
        if (this.data !== undefined) {
            this.currentImgIndx = 1;
            this.totalImgIndx = this.data.length;
        }

        if (this.plt.is('cordova')) {
            this.isNative = true;
        } else {
            this.isNative = false;
        }

        setTimeout(() => {// 滑动之前未初始化
            if (this.activeIndex >= 0) {
                this.loopSlider.slideTo(this.activeIndex);
                console.log('message-----> s2=' + this.activeIndex);
            }
        }, 500);
    }

    onSlideChanged(slider: IonSlides) {
        this.loopSlider.getActiveIndex().then(data => {
            this.currentImgIndx = data + 1;
        });
    }

    ngAfterViewInit() {
    }

    dismiss() {
        if (this.forPdf) {
            this.modalCtrl.dismiss(this.pdfData);
        } else {
            this.modalCtrl.dismiss(this.data);
        }
    }


    async doDelete() {
        const alert = await this.alert.create({
            message: '确认删除这张图片？',
            buttons: [
                {
                    text: '取消',
                    role: 'cancel',
                    cssClass: 'secondary',
                    handler: () => {
                        console.log('Confirm Cancel');
                    }
                }, {
                    text: '确定',
                    cssClass: 'LJ',
                    handler: () => {
                        // last data
                        if (this.currentImgIndx === 1 && this.data.length === 1) {
                            this.currentImgIndx = 0;
                            this.sliderHome.slidesItems = [];
                            this.data = [];
                            if (this.forPdf) {
                                this.pdfData.valueLongArray = [];
                                this.pdfData.valueIndexArray = [];
                                this.pdfData.valueArray = [];
                            }
                            this.totalImgIndx = 0;
                            this.loopSlider.update().then(data => {
                                this.dismiss();
                            });
                        } else {

                            const deleteIndex = this.currentImgIndx - 1;
                            if (this.currentImgIndx === this.data.length) {
                                this.currentImgIndx--;
                            }
                            this.data.splice(deleteIndex, 1);
                            if (this.forPdf) {
                                this.pdfData.valueLongArray.splice(deleteIndex, 1);
                                this.pdfData.valueIndexArray.splice(deleteIndex, 1);
                                this.pdfData.valueArray.splice(deleteIndex, 1);
                            }
                            this.totalImgIndx = this.data.length;
                            this.sliderHome.slidesItems = this.data;
                            this.loopSlider.update().then(data => {
                                if (this.sliderHome.slidesItems === 0) {
                                    this.dismiss();
                                } else {
                                    this.loopSlider.slideTo(this.currentImgIndx - 1);
                                }
                            });
                        }
                    }
                }
            ]
        });
        await alert.present();
    }
}
